const exec=require("../DBhelper").exec
const jwt=require("jwt-simple")

class Users{
    async login(req,res){
        let sql="select id,avatar,username,phone,createtime from users where status=1 and username=? and password=?";
        let params=[req.body.username,req.body.password]
        let result=await exec(sql,params)
        let isLogin=result&&result.length>=1
        if(isLogin){
            let token=jwt.encode(JSON.stringify(result[0]),"cs2209")
            res.json({msg:"登录成功",code:1,data:result[0],token:token,user:req.body.username})
        }else{
            res.json({msg:"用户名或密码错误",code:-1})
        }
    }
}

module.exports=new Users()